<script setup lang="ts">
import { computed } from 'vue';
import { Handle, Position, useVueFlow } from '@vue-flow/core';

interface NodeData {
  ip: string;
  port: number | string;
  cpu: string;
  memory: string;
  status: string;
}

interface Props {
  data: NodeData;
}

const props = defineProps<Props>();
const emit = defineEmits(['dblclick']);
const { getEdges } = useVueFlow();

// 检查是否已有映射关系
const isMapped = computed(() => {
  const edges = getEdges.value;
  return edges.some(edge => edge.target === `node-${props.data.ip}-${props.data.port}`);
});
</script>

<template>
  <div class="node-node" :class="{ 'is-mapped': isMapped }" @dblclick="emit('dblclick', props.data)">
    <div class="node-header">
      <div class="node-icon">
        <icon-ic-round-dns class="text-16px" />
      </div>
      <div class="node-title">{{ data.ip }}:{{ data.port }}</div>
      <div class="node-status">
        <ATag color="green" size="small">在线</ATag>
      </div>
    </div>
    <div class="node-content">
      <div class="node-info">
        <div class="info-item">
          <span class="label">CPU:</span>
          <span class="value">{{ data.cpu }}</span>
        </div>
        <div class="info-item">
          <span class="label">内存:</span>
          <span class="value">{{ data.memory }}</span>
        </div>
      </div>
      <div class="node-actions"></div>
    </div>
    <Handle :id="`node-${data.ip}-${data.port}`" type="target" :position="Position.Left" class="node-handle" />
  </div>
</template>

<style scoped>
.node-node {
  background: #fff;
  border: 2px solid #e5e7eb;
  border-radius: 8px;
  padding: 12px;
  min-width: 200px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: all 0.2s;
}
.dark .node-node {
  background: #1e293b;
  border-color: #334155;
  color: #f8fafc; /* 浅色文本，确保在深色背景上可见 */
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.node-node:hover {
  border-color: #52c41a;
  box-shadow: 0 4px 8px rgba(82, 196, 26, 0.15);
}

.node-node.is-mapped {
  border-color: #1890ff;
  background: linear-gradient(135deg, #f0f8ff 0%, #ffffff 100%);
}
.dark .node-node.is-mapped {
  border-color: #4096ff;
  background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
}
.node-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.node-icon {
  color: #52c41a;
}

.node-title {
  font-weight: 600;
  font-size: 14px;
  color: #262626;
  flex: 1;
  font-family: 'Courier New', monospace;
}
.dark .node-title {
  color: #f8fafc;
}

.node-content {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.node-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.info-item {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
}

.info-item .label {
  color: #8c8c8c;
}
.dark .info-item .label {
  color: #bababa;
  font-weight: 500;
}
.info-item .value {
  color: #262626;
  font-weight: 500;
}

.dark .info-item .value {
  color: #f8fafc;
  font-weight: 500;
}

.node-actions {
  display: flex;
  justify-content: center;
}

.node-handle {
  width: 8px;
  height: 8px;
  background: #52c41a;
  border: 2px solid #fff;
  box-shadow: 0 0 0 2px #52c41a;
}

.node-handle:hover {
  background: #73d13d;
  box-shadow: 0 0 0 2px #73d13d;
}
</style>
